package com.luntan.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.luntan.bean.Forumbean;

import android.os.StrictMode;

public class Utils {
 
	public static int i;
	public static String getCurrentTime(String format) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
		String currentTime = sdf.format(date);
		return currentTime;
	}

	public static String getCurrentTime() {
		return getCurrentTime("yyyy-MM-dd  HH:mm:ss");

	}

	public static String getHttpJSON(String url) {

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());

		HttpGet httpRequest = new HttpGet(url);
		try {
			HttpParams params = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(params, 5 * 1000);
			HttpConnectionParams.setSoTimeout(params, 8 * 1000);
			HttpConnectionParams.setSocketBufferSize(params, 8192);

			HttpClient httpclient = new DefaultHttpClient(params);
			HttpResponse httpResponse = httpclient.execute(httpRequest);

			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				String jsonStr = EntityUtils.toString(httpResponse.getEntity(),
						"UTF-8");
				return jsonStr;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("error on getHttpJSON : " + e.getMessage());
		}
		return null;
	}

	public static ArrayList<Forumbean> getList(
			String jsonStr) {
		// TODO Auto-generated method stub
		ArrayList<Forumbean> retList=new   ArrayList<Forumbean>();
		try {
			JSONArray jsonArray = new JSONArray(jsonStr);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				Forumbean bean = new Forumbean();
				bean.setName("");
				bean.setContent("");
				bean.setTime("");
				retList.add(bean);
				
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return retList;
	}
}
